﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using System.Data;

public partial class IFRS_SEC03_2E : PageBase
{
    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            this.SetErrorMessage();
            this.AddClientScript();
            this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }
    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/SEC03_1S.aspx");
    }

    #endregion
    #region "----- Methods -------"
    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsMenu.SEC01_1S;
    }
    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqEmployee_ID.SetRequiredField(lblEmployee_ID, txtEmployee_ID, btnSAVE);
        reqGroup.SetRequiredField(lblGroup, txtGroup, btnSAVE);
    }
    private void AddClientScript()
    {
        btnGroup.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/RoleGroup.aspx") + "','700','550');");

        txtGroup.AddControl(hidGroup_ID.ClientID);
        txtGroup.AddControl(txtGroup.ClientID);
        txtGroup.ControlSearch = txtGroup.ClientID;
        txtGroup.SearchKey = "RoleGroup_oRoleGroupID_oRoleGroupName";
    }
    private void BindData(int ID)
    {
        var vROLEGROUPUSER = new SPR_ROLEGROUPUSER();
        DataTable dtvROLEGROUPUSER = vROLEGROUPUSER.SEARCH_BYID(ID);

        if (dtvROLEGROUPUSER.Rows.Count > 0)
        {
            hidRoleGroupUserID.Value = dtvROLEGROUPUSER.Rows[0]["RoleGroupUserID"].ToString();
            hidGroup_ID.Value = dtvROLEGROUPUSER.Rows[0]["RoleGroupID"].ToString();
            hidEmployee_Name.Value = dtvROLEGROUPUSER.Rows[0]["Username"].ToString();
            txtGroup.Text = dtvROLEGROUPUSER.Rows[0]["RoleGroupName"].ToString();
            txtEmployee_ID.Text = dtvROLEGROUPUSER.Rows[0]["EmployeeCode"].ToString();
            txtEmployee_Name.Text = dtvROLEGROUPUSER.Rows[0]["description"].ToString();
            txtCostCenter.Text = dtvROLEGROUPUSER.Rows[0]["COST_CENTER_NM"].ToString();


            if (dtvROLEGROUPUSER.Rows[0]["IsActive"].ToString().ToLower() == "true")
            {
                rbSTATUS_1.Checked = true;
            }
            else
            {
                rbSTATUS_2.Checked = true;
            }

        }
    }
    private void SaveData()
    {

        try
        {
            RoleGroupUser vRoleGroupUser = new RoleGroupUser();
            vRoleGroupUser.RoleGroupUserID = hidRoleGroupUserID.Value.AsInt32();
            vRoleGroupUser.EmployeeCode = txtEmployee_ID.Text;
            vRoleGroupUser.RoleGroupID = hidGroup_ID.Value.AsInt32();
            vRoleGroupUser.Username = hidEmployee_Name.Value.ToString();
            if (rbSTATUS_1.Checked)
            {
                vRoleGroupUser.IsActive = true;
            }
            else
            {
                vRoleGroupUser.IsActive = false;
            }
            vRoleGroupUser.CreatedDate = DateTime.Now;
            vRoleGroupUser.CreatedBy = SessionManager.IFRS_EMPLOYEE_ID;
            vRoleGroupUser.CreatedCostCenter = SessionManager.IFRS_COST_CENTER;
            vRoleGroupUser.CreatedBranch = SessionManager.IFRS_BRANCH;
            vRoleGroupUser.UpdatedDate = DateTime.Now;
            vRoleGroupUser.UpdatedBy = SessionManager.IFRS_EMPLOYEE_ID;
            vRoleGroupUser.UpdatedCostCenter = SessionManager.IFRS_COST_CENTER;
            vRoleGroupUser.UpdatedBranch = SessionManager.IFRS_BRANCH;

            var vROLEGROUPUSER = new SPR_ROLEGROUPUSER();
            int iID = vROLEGROUPUSER.UPDATE(vRoleGroupUser);
            if (iID > 0)
            {
                // Utility.AlertMessage(Page, Resources.ifrsResource.SaveData);
                this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/SEC03_2E.aspx?&id={0}", hidRoleGroupUserID.Value.AsInt32())));
                //Response.Redirect(string.Format("~/IFRS/SEC01_2E.aspx?id={0}", iID)); 
            }
            else
            {
                Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
    #endregion
}